package com.hks360.car_treasure.mvp.user.presenter;

import android.content.Context;

import com.hks360.car_treasure.model.Add.User;
import com.hks360.car_treasure.model.BaseModel;
import com.hks360.car_treasure.mvp.BaseModelListener;
import com.hks360.car_treasure.mvp.user.model.UserModel;
import com.hks360.car_treasure.mvp.user.model.UserModelImpl;
import com.hks360.car_treasure.mvp.user.view.UserView;

/**
 * Created by xiaote on 2016/3/29.
 */
public class UserPresenterImpl implements UserPresenter {
    private UserModel userModel;
    private UserView userView;

    public UserPresenterImpl(Context context, UserView userView) {
        userModel = new UserModelImpl(context);
        this.userView = userView;
    }

    @Override
    public void login(int loginWhat) {
        UserModelImpl.LoginListener loginListener = new UserModelImpl.LoginListener() {
            @Override
            public void onSuccess(int what, User user) {
                if (user != null) {
                    if (user.getErrcode() == 0) {
                        if(what != 1){
                            userView.showToastMsg("登录成功！");
                        }
                        userView.loginSuccess(user);
                    } else {
                        if(what == 1){
                            userView.clearView();
                        } else {
                            userView.showToastMsg(user.getErrmsg());
                        }
                    }
                }
            }

            @Override
            public void onFailed(int what) {
                userView.clearView();
                if(what != 1){
                    userView.showToastMsg("登录失败，请稍后重新登录！");
                }
            }

            @Override
            public void onStart(int what) {
                userView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                userView.hideLoading();
            }
        };
        userModel.login(loginWhat,userView.getMobile(), userView.getPassword(), loginListener);
    }


    public void regist(final int registWhat, final int loginWhat) {
        final UserModelImpl.LoginListener loginListener = new UserModelImpl.LoginListener() {
            @Override
            public void onSuccess(int what, User user) {
                if (what == registWhat) {
                    if (user != null && user.getErrcode() == 0) {
                        userModel.login(loginWhat,userView.getMobile(), userView.getPassword(), this);
                    } else {
                        userView.hideLoading();
                        userView.showToastMsg(user.getErrmsg());
                    }
                } else if (what == loginWhat) {
                    userView.loginSuccess(user);
                }
            }

            @Override
            public void onFailed(int what) {
                userView.clearView();
                if (what == loginWhat || what == registWhat) {
                    userView.hideLoading();
                    userView.showToastMsg("注册失败！");
                }
            }

            @Override
            public void onStart(int what) {
                if (what == registWhat) {
                    userView.showLoading();
                }
            }

            @Override
            public void onFinish(int what) {
                if (what == loginWhat) {
                    userView.hideLoading();
                }
            }
        };
        userModel.regist(registWhat,userView.getMobile(), userView.getPassword(), userView.getCode(), loginListener);
    }

    @Override
    public void sendCheckCode(int what) {
        final BaseModelListener listener = new BaseModelListener() {
            @Override
            public void onSuccess(int what,BaseModel baseModel) {
                if(baseModel.getErrcode() == 0) {
                    userView.sendCheckCodeSuccess();
                } else {
                    userView.sendCheckCodeFailed();
                }
            }

            @Override
            public void onFailed(int what) {
                userView.sendCheckCodeFailed();
            }

            @Override
            public void onStart(int what) {

            }

            @Override
            public void onFinish(int what) {

            }
        };
        userModel.sendCheckCode(what, userView.getMobile(), listener);
    }

    @Override
    public void changePassword(int what) {
        final BaseModelListener listener  = new BaseModelListener() {
            @Override
            public void onSuccess(int what, BaseModel model) {
                if(model.getErrcode() == 0) {
                    userView.findPasswordSuccess();
                    userView.showToastMsg("新密码设置成功！");
                } else {
                    userView.showToastMsg(model.getErrmsg());
                }
            }

            @Override
            public void onFailed(int what) {
                userView.clearView();
                userView.showToastMsg("修改密码失败，请稍后重新尝试！");
            }

            @Override
            public void onStart(int what) {
                userView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                userView.hideLoading();
            }
        };
        userModel.changePassword(what,userView.getMobile(),userView.getCode(),userView.getPassword(),listener);
    }
}
